package com.samphanie.auiu.auth.controller;

import com.samphanie.auiu.auth.service.IUserService;
import com.samphanie.auiu.auth.utils.SecurityUtil;
import com.samphanie.auiu.auth.utils.UserUtil;
import com.samphanie.auiu.common.dto.UserProfileDto;
import com.samphanie.auiu.common.exception.ApiException;
import com.samphanie.auiu.common.utils.ApiResponse;
import com.samphanie.auiu.common.utils.ResultCode;
import com.samphanie.auiu.mbg.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;
import java.util.stream.Collectors;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Slf4j
@Api(tags = "用户模块")
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    @ApiOperation("用户信息")
    @GetMapping("/me")
    public ApiResponse<?> getProfile() {

        return userService.findOptionalByUsername(SecurityUtil.getCurrentUsername())
                .map(user -> {
                            UserProfileDto profileDto = UserProfileDto.builder()
                                    .id(user.getId())
                                    .username(user.getUsername())
                                    .nickname(user.getNickname())
                                    .avatar(user.getAvatar())
                                    .email(user.getEmail())
                                    .mobile(user.getMobile())
                                    .roles(user.getRoles().stream().map(role -> UserUtil.fromRole.apply(role)).collect(Collectors.toList()))
                                    .build();
                    log.info("获取到当前用户信息: {}", user.getId());
                            return ApiResponse.success(profileDto);
                        }
                ).orElseThrow(
                        () -> new ApiException(ResultCode.INTERNAL_SERVER_ERROR)
                );
    }

    @ApiOperation("编辑用户信息")
    @PostMapping("/me")
    public ApiResponse<?> saveProfile(@RequestBody UserProfileDto userProfileDto, Principal principal) {
        return userService.findOptionalByUsername(principal.getName())
                .map(user -> {
                            User saveUser = userService.saveUser(user
                                    .withNickname(userProfileDto.getNickname())
                                    .withEmail(userProfileDto.getEmail())
                                    .withMobile(userProfileDto.getMobile()));
                            return ApiResponse.success(saveUser);
                        }
                ).orElseThrow(
                        () -> new ApiException(ResultCode.INTERNAL_SERVER_ERROR)
                );
    }

    @ApiOperation("获取用户名")
    @GetMapping("/users/getUsername")
    public ApiResponse<?> getCurrentUsername(@ApiIgnore Principal principal) {
        return ApiResponse.success(principal.getName());
    }

    @ApiOperation("获取邮箱")
    @GetMapping("/users/by-email/{email}")
    public ApiResponse<?> getUserByEmail(@PathVariable String email) {
        return userService.findOptionalByEmail(email)
                .map(ApiResponse::success)
                .orElseThrow(
                        () -> new ApiException(ResultCode.INTERNAL_SERVER_ERROR)
                );
    }

}
